package com.lx.user.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lx.bean.Result;
import com.lx.bilibili.entity.Notice;
import com.lx.user.dao.NoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeMapper noticeMapper;

    @RequestMapping("/getNotice")
    public List<Notice> getNotice(int id) {
        LambdaQueryWrapper<Notice> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Notice::getUid, id);
        // 分页查询 最后三条数据
        lqw.orderByDesc(Notice::getTime);
        lqw.last("limit 0,3");
        return noticeMapper.selectList(lqw);
    }

    @RequestMapping("/addNotice")
    public Result addNotice(@RequestBody Notice notice) {
        notice.setTime(LocalDateTime.now());
        noticeMapper.insert(notice);
        return new Result(1, "添加成功", null);
    }
}
